# -*- coding:utf-8 -*-

import pygame
import random
import math
import copy
from pygame.locals import *

width = 1200
height = 800

radius = 5
march_step = 2

start_pos = [10, 10]
end_pos = [200, 200]

# k = (end_pos[1] - start_pos[1]) / (end_pos[0] - start_pos[0])


# def get_new_pos(old_pos):
#     return [old_pos[0] + 1 * (k / abs(k)), old_pos[1] + 1 * k]


class PlayerMarch:
    def __init__(self):
        """
        init the march
        """
        self.start = start_pos
        self.end = end_pos
        self.k = 0
        self.n = 0      # march move count
        self.current = start_pos

    def begin(self, start, end):
        """
        begin the march
        """
        self.start = start
        self.end = end
        self.k = float(self.end[1] - self.start[1]) / (self.end[0] - self.start[0])  # gradient
        self.current = self.start

    def next_pos(self):
        """
        change and return the march's next position
        """
        self.n = self.n + 1
        # k_next = float(self.end[1] - self.current[1]) / (self.end[0] - self.current[0])
        self.current = [int(round(self.start[0] + march_step * self.n * (self.k / abs(self.k)))),
                        int(round(self.start[1] + march_step * self.n * self.k))]
        return self.current


# generate monsters on map randomly
def gen_monster():
    list_monster = []
    for x in range(1, width, 10):
        for y in range(1, height, 10):
            if random.randint(1, 200) == 1:  # 1/10 percent
                list_monster.append([x, y])
    return list_monster


def main():
    pygame.init()
    clock = pygame.time.Clock()

    screen = pygame.display.set_mode([width, height])

    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((255, 255, 255))

    restart = True
    while restart:
        screentitle = pygame.display.set_caption("slg map")
        player_pos = start_pos

        monster_pos_list = gen_monster()
        monster_rect_list = []

        march_list = []

        running = True
        while running:
            # fill the background is white
            screen.fill([255, 255, 255])

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    exit(0)
                elif event.type == MOUSEMOTION:
                    pass
                elif event.type == MOUSEBUTTONDOWN:
                    # return the X and Y position of the mouse cursor
                    # pos = pygame.mouse.get_pos()
                    # mouse_x = pos[0]
                    # mouse_y = pos[1]
                    # pressed_array = pygame.mouse.get_pressed()
                    # for index in range(len(pressed_array)):
                    # if pressed_array[index]:
                    #     if index == 0:
                    #         print('Pressed LEFT Button!')
                    #     elif index == 1:
                    #         print('The mouse wheel Pressed!')
                    #     elif index == 2:
                    #         print('Pressed RIGHT Button!')
                    pos = pygame.mouse.get_pos()
                    mouse_x = pos[0]
                    mouse_y = pos[1]
                    if mouse_x > 0 and mouse_y > 0:
                        for monster_pos in monster_pos_list:
                            # if monster_rect.colliderect(end_rect):
                            if abs(mouse_x - monster_pos[0]) < 5 and abs(mouse_y - monster_pos[1]) < 5:
                                player_march = PlayerMarch()
                                player_march.begin(start_pos, monster_pos)
                                march_list.append(player_march)
                                break

            time_pass = clock.tick(20)

            pygame.draw.line(screen, [0, 255, 0], start_pos, end_pos, 2)
            start_rect = pygame.draw.circle(screen, [0, 0, 255], start_pos, radius, 0)
            end_rect = pygame.draw.circle(screen, [0, 0, 255], end_pos, radius, 0)

            for monster_pos in monster_pos_list:
                monster_rect = pygame.draw.circle(screen, [255, 0, 255], monster_pos, radius, 0)
                if len(monster_rect_list) < len(monster_pos_list):
                    monster_rect_list.append(monster_rect)

            march_list_new = []
            for player_march in march_list:
                pygame.draw.line(screen, [0, 255, 0], player_march.start, player_march.end, 2)

                march_pos_new = player_march.next_pos()
                player_rect = pygame.draw.circle(screen, [255, 0, 0], march_pos_new, radius, 0)
                if not player_rect.colliderect(player_march.end[0] - 5, player_march.end[1] - 5, 10, 10):
                    march_list_new.append(player_march)

            march_list = march_list_new

            # player_pos = get_new_pos(player_pos)
            # player_rect = pygame.draw.circle(screen, [255, 0, 0], player_pos, radius, 0)

            # if player_rect.colliderect(end_rect):
            #     running = False
            pygame.display.update()
        # game over background
        pygame.font.init()
        screen.fill([100, 0, 0])
        font = pygame.font.Font(None, 48)
        text = font.render("Game Over !!!", True, (255, 0, 0))
        textRect = text.get_rect()
        textRect.centerx = screen.get_rect().centerx
        textRect.centery = screen.get_rect().centery + 24
        screen.blit(text, textRect)
        # keydown r restart,keydown n exit
        while 1:
            event = pygame.event.poll()
            if event.type == pygame.QUIT:
                pygame.quit()
                exit(0)
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_r:
                    restart = True
                    break
                if event.key == pygame.K_n:
                    restart = False
                    break
            pygame.display.update()


if __name__ == '__main__':
    main()
